package com.xzqwjw.taskmanager.activity;

import java.util.List;
import java.util.Map;

import com.xzqwjw.taskmanager.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ActivityQueryResult extends Activity {

  private String strDBFile = "";
  private int intQueryKey = 0;
  private int intUnitID = 0;
  private String strWhere = "";
  private int intQueryFlag = 0;
  private List<Map<String, Object>> listData = null;

  private TextView tvQueryResult = null;
  private ListView lvQueryResult = null;

  private ProgressDialog objPD;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_query_result);

    tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
    lvQueryResult = (ListView) findViewById(R.id.lvQueryResult);

    Intent objIT = getIntent();
    strDBFile = objIT.getStringExtra("DBFilePath");
    intQueryKey = objIT.getIntExtra("QueryKey", 0);
    intUnitID = objIT.getIntExtra("UnitID", 0);
    strWhere = objIT.getStringExtra("Where");
    new AsyncLoadQueryResult().execute();
  }

  class ItemClickListener implements OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> avParent, View objView, int intPosition, long lngRowID) {

      @SuppressWarnings("unchecked")
      Map<String, Object> objMap = (Map<String, Object>) avParent.getItemAtPosition(intPosition);

      if (objMap != null) {
        // ��ȡ����Դ������ֵ
        String strAID = (String) objMap.get("AID");

        Intent objIT = new Intent(ActivityQueryResult.this, ActivityShowInfo.class);
        objIT.putExtra("AID", strAID);
        objIT.putExtra("DBFilePath", strDBFile);
        startActivity(objIT);

      } else {
        Toast.makeText(ActivityQueryResult.this, "null", Toast.LENGTH_SHORT).show();
      }

    }
  }

  class AsyncLoadQueryResult extends android.os.AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
      objPD = ProgressDialog.show(ActivityQueryResult.this, "��ȡ����", "���ڸ�������������Ա���ݣ����Եȣ�", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {

      // QueryKey:
      // 0:�õ���������
      // 1:���ݵ�λ��ѯ
      // 2:condition1
      // 3:�����Զ���������ѯcondition
      // �����unit�������AID

      switch (intQueryKey) {
        case 0:
          intQueryFlag = 0;
          listData = com.leaderinfo.service.ItemDataService.getAllData(strDBFile);
          break;
        case 1:
          intQueryFlag = 0;
          // listData =
          // ServiceItemData.getInfosByUnitID(strDBFile,intUnitID);
          listData = null;
          break;
        case 2:
          intQueryFlag = 0;
          listData = com.leaderinfo.service.ItemDataService.getInfosByWhere(strDBFile, strWhere);
          break;
        case 3:
          intQueryFlag = 1;
          listData = com.leaderinfo.service.ItemDataService.getInfosByWhere1(strDBFile, strWhere);
          break;
        default:
          intQueryFlag = 0;
          listData = com.leaderinfo.service.ItemDataService.getAllData(strDBFile);
          break;
      }
      return null;
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if (listData != null) {
        if (listData.size() > 0) {
          tvQueryResult.setText("����ѯ��" + listData.size() + "����¼��");
          if (intQueryFlag == 0) {
            com.leaderinfo.utility.ListItemPartyInfoAdapter objAdapter = new com.leaderinfo.utility.ListItemPartyInfoAdapter(ActivityQueryResult.this,
                listData, strDBFile);
            lvQueryResult.setAdapter(objAdapter);
          } else if (intQueryFlag == 1) {
            com.leaderinfo.utility.ListItemQueryResultAdapter objAdapter = new com.leaderinfo.utility.ListItemQueryResultAdapter(ActivityQueryResult.this,
                listData, strDBFile);
            lvQueryResult.setAdapter(objAdapter);
          }
          // ��ӵ���¼�����
          lvQueryResult.setOnItemClickListener(new ItemClickListener());

        } else {
          tvQueryResult.setText("û�в�ѯ�����������ļ�¼�����������ò�ѯ������");
          new AlertDialog.Builder(ActivityQueryResult.this).setTitle("ϵͳ��ʾ")
              .setMessage("û�в�ѯ�����������ļ�¼�����������ò�ѯ������")
              .setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                  finish();
                }
              }).show();
        }
      } else {
        new AlertDialog.Builder(ActivityQueryResult.this).setTitle("ϵͳ��ʾ").setMessage("û�в�ѯ�����������ļ�¼�����������ò�ѯ������")
            .setPositiveButton("ȷ��", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                finish();
              }
            }).show();
      }
      objPD.dismiss();
    }

  }

}
